package com.lsyw.intergrate.common.generator;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liulei
 * @version 1.0
 * @date 2022/3/8 16:10
 */
public class CodeGenerator {
    /**
     * 代码生成器
     */
    public static void generate() {
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        //String projectPath = GeneratorUtil.class.getProtectionDomain().getCodeSource().getLocation().getPath();
        String projectPath = GeneratorConf.projectPath;
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor(GeneratorConf.author);
        gc.setMapperName(GeneratorConf.mapperName);
        gc.setServiceName(GeneratorConf.serviceName);
        gc.setServiceImplName(GeneratorConf.serviceImplName);
        gc.setXmlName(GeneratorConf.xmlName);
        gc.setOpen(false);
        gc.setEnableCache(false);
        //是否覆盖已生成的文件
        gc.setFileOverride(true);
        //实体属性 Swagger2 注解
        //gc.setSwagger2(true);
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL);
        dsc.setUrl(GeneratorConf.url);
        // dsc.setSchemaName("public");
        dsc.setDriverName(GeneratorConf.driverName);
        dsc.setUsername(GeneratorConf.username);
        dsc.setPassword(GeneratorConf.password);
        dsc.setTypeConvert(new MySqlTypeConvert() {
            @Override
            public DbColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) {
                System.out.println("转换类型：" + fieldType);
                //tinyint转换成Boolean
                if (fieldType.toLowerCase().contains("tinyint")) {
                    return DbColumnType.BOOLEAN;
                }
                //将数据库中datetime转换成date
                if (fieldType.toLowerCase().contains("datetime")) {
                    return DbColumnType.DATE;
                }
                if (fieldType.toLowerCase().contains("int")) {
                    return DbColumnType.BASE_INT;
                }
                return (DbColumnType) super.processTypeConvert(globalConfig, fieldType);
            }
        });
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent(GeneratorConf.basePackage)
                .setController(GeneratorConf.controllerPackageName)
                .setService(GeneratorConf.servicePackageName)
                .setServiceImpl(GeneratorConf.serviceImplPackageName)
                .setMapper(GeneratorConf.mapperPackageName)
                .setXml(GeneratorConf.xmlPackageName)
                .setEntity(GeneratorConf.entityPackageName);
        mpg.setPackageInfo(pc);

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                //  TODO  to do nothing
            }
        };


        // 自定义输出配置
        List<FileOutConfig> focList = new ArrayList<>();
        // 自定义配置会被优先输出
        //指定自定义文件输出路径，是需要带上.ftl/.vm, 注意和下面的模板配置做区分
        focList.add(new FileOutConfig(GeneratorConf.xmlTemplatePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输出文件名 ， 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化！！
                return GeneratorConf.projectPath + "/src/main/resources/mapper/" +
                        "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });

        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);

        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig();

        // 配置自定义输出模板
        //指定自定义模板路径，注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别

        templateConfig
                .setEntity(GeneratorConf.entityTemplatePath)
                .setMapper(GeneratorConf.mapperTemplatePath)
                .setService(GeneratorConf.serviceTemplatePath)
                .setServiceImpl(GeneratorConf.serviceImplTemplatePath)
                .setController(GeneratorConf.controllerTemplatePath)
                .setXml(null);
        mpg.setTemplate(templateConfig);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setRestControllerStyle(true);
        strategy.setEntityLombokModel(true);
        strategy.setInclude(GeneratorConf.tableNames);
        mpg.setStrategy(strategy);

        mpg.setTemplateEngine(new VelocityTemplateEngine());
        // mpg.setTemplateEngine(new FreemarkerTemplateEngine());

        //执行
        mpg.execute();
    }

    public static void main(String[] args) {
        CodeGenerator.generate();
    }

}
